So let's see how inheritance
works in python. So you can have one class
inherit from another class. That means that the subclass or child class inherits
attributes as well as methods. So let's look at this. So here we have an example
of inheritance and you can use this URL to open this
up in visual in the python tutor. And here we have a class person
that has a first and last name. We can print the first and last name and
if you print the object of the class, we can get the initials. We can also declare a class employee
that inherits from the class person. So in the parentheses
after class employee, we say who are we inheriting from? That means we can get all
their attributes and methods. And that means that if I call the same
method name on the employee class, it'll look for
the employee class first for that method. And that would be executed instead of
an inherited one from the person class. But I can still call an inherited
method if I use super parent to get back an object of the parent class. To get back the parent class and
then I can call methods on that. And so, let's see how this works. We're going to go to
python tutor to that URL. So if I say next, first thing to
do is declare the class person, so we see they have the unit method,
the string method, the initials method. We call the class employee and notice
it says employee class extends person. So as a child of subclass of. And then in our main method, we define our
main method and we say okay, if this is the current thing that's executing,
let's go ahead and execute the main. So that will go and execute the main. First thing we do is create
an object of the person class. When we do that of course, we will go to
the init method in person and we'll go ahead and initialize this person instance
to have that first name and last name. We can see now we've copied these
local variables into that object. And then we're going to print that person
which will call the string method. So that's going to return that
name plus the self.first name plus a space plus self.last name. And we see that get printed. When we're going to call print
the result of calling p.initials. So we go call p.initials. And we see we're going to return the first
character in the first name plus the first character in the last name. So we see that return value BE and
we print that. Now we're going to create
an object of the class employee. So again,
we go now to the init method employee, notice we don't start at the one
in person, this is an employee. So we started the init method in employee. And because we want to set that first and
last name, we can just reuse that code
from the person class. So we use super print print.underscore to
call that init method in the person class. So when I execute this,
we'll go to that init method in person. And that will again in
this employee instance, it'll initialize the first name,
then the last name. And then all we have left
to initialize is the ID. So we go back to the init method in the
employee class and we initialize the ID. And so now we have this here,
In this object. And now if we print that object,
we're going to go, There is no string method in the employee
class, so we don't override it. So we're going to go ahead and
execute the one in the person class. So that will return and
print the first name and last name. And then we can ask again for
the initials. Again, that's something that inherited,
employee inherited from person. So to go and
execute that in the person class and it will return those initials MA and
we'll see those print. So that's how inheritance works in python.